<template>
  <div>
    <el-form ref="loginForm" :model="form" :rules="rules" label-width="80px" class="login-box">
      <h3 class="login-title">管理员登录</h3>
      <el-form-item label="账号" prop="username">
        <el-input type="text" maxlength=18 placeholder="请输入用户名" v-model="form.username" prefix-icon="icon-login_user" clearable/>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input type="password" maxlength="18" placeholder="请输入密码" v-model="form.password" prefix-icon="icon-login_pwd" clearable/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" round class="btnlocation" v-on:click="onSubmit('loginForm')">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  export default {
    name: "Login",
    data() {
      return {
        form: {
          username: 'admin',
          password: 'admin',
        },

        // 表单验证，需要在 el-form-item 元素中增加 prop 属性
        rules: {
          username: [
            {required: true, message: '账号不可为空', trigger: 'blur'}
          ],
          password: [
            {required: true, message: '密码不可为空', trigger: 'blur'}
          ]
        },

        // 对话框显示和隐藏
        dialogVisible: false
      }
    },
    methods: {
      onSubmit(formName) {
        // 为表单绑定验证功能
        this.$refs[formName].validate((valid) => {
          if (valid) {
            // 使用 vue-router 路由到指定页面，该方式称之为编程式导航
            this.$axios.get('/adminlogin', {
              params:{
                username: this.form.username,
                password: this.form.password,
              }
            })
              .then(res =>{
                console.log(res.data.code);
                if(res.data.code === 200){
                  this.$store.commit('SET_TOKEN',res.data.data.token);
                  this.$store.commit('GET_USER',res.data.data.name);
                  this.$store.commit('SET_UID',res.data.data.uid);
                  this.$store.commit("SET_ROLE",res.data.data.roleid);
                  console.log(res.data.roleid);
                  this.$message({
                    duration:1000,
                    message: '登陆成功',
                    type: 'success'
                  });
                  this.$router.push("/home");
                }
                else if(res.data.code === 400){
                  console.log(res.data.code);
                  this.$message({
                    duration:2000,
                    message: res.data.msg,
                    type: 'error'
                  })
                }else if(res.data.code === 401){
                  console.log(res.data.code);
                  this.$message({
                    duration:2000,
                    message: res.data.msg,
                    type: 'error'
                  })
                }else{
                  console.log(res.data.code);
                  this.$message({
                    duration:1500,
                    message: "用户名或密码错误",
                    type: 'error'
                  })
                }
              })
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  }
</script>

<style lang="scss" scoped>
  .login-box {
    border: 10px solid #DCDFE6;
    width: 400px;
    margin: 200px auto;
    padding: 40px 60px 30px 40px;
    border-radius: 20px;
    -webkit-border-radius: 20px;
    -moz-border-radius: 5px;
    box-shadow: 0 0 25px #909399;
  }
  .login-title {
    text-align: center;
    margin: 0 auto 40px 40px;
    color: #303133;
  }
  .btnlocation{
    margin: 8px 35px 10px auto;
  }
  .el-radio{
    margin: 0 auto 10px 30px;
  }
</style>
